package com.hzncc.flowable_diboot.service.impl;

import com.diboot.core.util.BeanUtils;
import com.diboot.file.entity.FileRecord;
import com.diboot.file.service.FileRecordService;
import com.diboot.file.service.FileStorageService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import com.diboot.core.service.impl.BaseServiceImpl;

import com.hzncc.flowable_diboot.entity.CustomerPortrait;
import com.hzncc.flowable_diboot.mapper.CustomerPortraitMapper;
import com.hzncc.flowable_diboot.service.CustomerPortraitService;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import jakarta.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.Optional;


/**
 * 客户画像 相关Service实现类
 *
 * @author MyName
 * @version 1.0
 * @date 2024-05-21
 * Copyright © MyCorp
 */
@Slf4j
@Service
public class CustomerPortraitServiceImpl extends BaseServiceImpl<CustomerPortraitMapper, CustomerPortrait> implements CustomerPortraitService {

    private final FileStorageService fileStorageService;

    public CustomerPortraitServiceImpl(FileStorageService fileStorageService, FileRecordService fileRecordService) {
        this.fileStorageService = fileStorageService;
    }

    @Override
    public String uploadFile(MultipartFile[] file) throws Exception {
        StringBuilder urls = new StringBuilder();
        for (MultipartFile multipartFile : file) {
            FileRecord record = fileStorageService.save(multipartFile);
            String accessUrl = record.getAccessUrl();
            urls.append(accessUrl).append(",");
        }
        return urls.substring(0, urls.length());
    }

    /**
     * 多文件上传
     *
     * @param request
     * @param params
     * @throws Exception
     * @author 陈广煜
     * @date 2024/5/24
     */
    public void uploadMultiFile(HttpServletRequest request, Map<String, Object> params) throws Exception {
        CustomerPortrait customerPortrait = BeanUtils.convertMap2Bean(params, CustomerPortrait.class);
        if (request instanceof MultipartHttpServletRequest) {
            // 客户图片
            List<MultipartFile> cp = ((MultipartHttpServletRequest) request).getFiles("cp");
            // 协议图片
            List<MultipartFile> dp = ((MultipartHttpServletRequest) request).getFiles("dp");
            // 协议附件
            List<MultipartFile> at = ((MultipartHttpServletRequest) request).getFiles("at");
            for (MultipartFile multipartFile : cp) {
                FileRecord fr = fileStorageService.save(multipartFile);
                String picture = customerPortrait.getCustomerPicture();
                customerPortrait.setCustomerPicture(Optional.ofNullable(picture).orElse("") + "," + fr.getId());
            }
            for (MultipartFile multipartFile : dp) {
                FileRecord fr = fileStorageService.save(multipartFile);
                String picture = customerPortrait.getDealPicture();
                customerPortrait.setDealAttach(Optional.ofNullable(picture).orElse("") + "," + fr.getId());
            }
            for (MultipartFile multipartFile : at) {
                FileRecord fr = fileStorageService.save(multipartFile);
                String attach = customerPortrait.getDealAttach();
                customerPortrait.setDealAttach(Optional.ofNullable(attach).orElse("") + "," + fr.getId());
            }
        }
        saveOrUpdate(customerPortrait);
    }

    @Override
    public void uploadMultiFile(MultipartFile[] cp, MultipartFile[] ap, MultipartFile[] at, CustomerPortrait customerPortrait) throws Exception {
        for (MultipartFile multipartFile : cp) {
            FileRecord fr = fileStorageService.save(multipartFile);
            String picture = customerPortrait.getCustomerPicture();
            customerPortrait.setCustomerPicture(Optional.ofNullable(picture).orElse("") + "," + fr.getId());
        }
        for (MultipartFile multipartFile : ap) {
            FileRecord fr = fileStorageService.save(multipartFile);
            String picture = customerPortrait.getDealPicture();
            customerPortrait.setDealPicture(Optional.ofNullable(picture).orElse("") + "," + fr.getId());
        }
        for (MultipartFile multipartFile : at) {
            FileRecord fr = fileStorageService.save(multipartFile);
            String attach = customerPortrait.getDealAttach();
            customerPortrait.setDealAttach(Optional.ofNullable(attach).orElse("") + "," + fr.getId());
        }
        saveOrUpdate(customerPortrait);
    }
}